//
//  MeViewController.m
//  Tour
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MeViewController.h"
#import "ZGSMyCollectionViewController.h"
#import "AdvanceEdit.h"
#import <MessageUI/MessageUI.h>
//#import "MobClick.h"
#import "UMSocial.h"
#import "AppDelegate.h"
@interface MeViewController ()<UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate, UIAlertViewDelegate>
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UMSocialSnsPlatform *snsPlatform;
@property (nonatomic, retain)UIImageView *image;
@property (nonatomic, retain)NSArray *tmpArr;
@end

@implementation MeViewController

- (void)dealloc{
    [_scrollView release];
    [_tableView release];
    [_arr release];
    [_snsPlatform release];
    [_image release];
    [_tmpArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    self.arr = [NSMutableArray array];
    self.tmpArr = @[@"电影收藏",@"清空缓存", @"反馈", @"夜间模式", @"版本检测"];
    self.arr = [NSMutableArray arrayWithArray:self.tmpArr];
    self.title = @"我的";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    UIView *headView = [[UIView alloc] init];
    headView.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 3);
    self.image = [[UIImageView  alloc] init];
    self.image.frame = CGRectMake(100, 100, 100, 100);
    self.image.center = headView.center;
    self.image.image = [UIImage imageNamed:@"10.jpeg"];
    self.image.layer.masksToBounds = YES;
//    image.layer.borderWidth = 1;
    self.image.layer.cornerRadius = 50;
    [headView addSubview:self.image];
    [_image release];
    
    self.tableView.tableHeaderView = headView;
    

    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.textLabel.text = self.arr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        
        ZGSMyCollectionViewController *shoucangVC = [[ZGSMyCollectionViewController alloc] init];
        shoucangVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:shoucangVC animated:YES];
    }
    else if (indexPath.row == 1) {
        // 创建一个文件管理者
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"%@", cachePath);
        CGFloat cacheSize = [self folderSizeAtPath:cachePath];
        
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@%.2fM", @"清除共", cacheSize] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.delegate = self;
        [alert show];
    }
    else if (indexPath.row == 2) {
        Class mailClass = NSClassFromString(@"MFMailComposeViewController");
        if (mailClass) {
            if ([mailClass canSendMail]) {
                //
                [self displayMailBox];
            }else{
                //
                [self showMailBoxOnDevice];
            }
        }else{
            [self showMailBoxOnDevice];
        }

    }
       else if (indexPath.row == 3) {
              [(AppDelegate *)[UIApplication sharedApplication].delegate change];
        if ([self.arr[indexPath.row]  isEqual: @"夜间模式"]) {
            [self.arr replaceObjectAtIndex:3 withObject:@"白天模式"];
            [self.tableView reloadData];

            }else{
                [self.arr replaceObjectAtIndex:3 withObject:@"夜间模式"];
                [self.tableView reloadData];


        }
        
    }else if (indexPath.row == 4) {
        NSString *version = [self getCurrentLocalVersion];
        NSLog(@"version: %@", version);
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"当前本版 :%@", version] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }
    
}

- (NSString *)getCurrentLocalVersion
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
    return appVersion;
}

- (void)showMailBoxOnDevice{
    NSString *mail = @"540699505@qq.com";
    mail = [mail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
}

- (void)displayMailBox {
    MFMailComposeViewController *mailBox = [[MFMailComposeViewController alloc] init];
    //收件地址
    NSArray *recipient = [NSArray arrayWithObject:@"540699505@qq.com"];
    [mailBox setToRecipients:recipient];
    //填写主题
    [mailBox setSubject:@"意见反馈"];
    //填写邮件内容
    NSString *messageBody = @"你的意见:";
    [mailBox setMessageBody:messageBody isHTML:YES];
    //添加一个文件
    UIImage *image = [UIImage imageNamed:@""];
    NSData *data = UIImagePNGRepresentation(image);
    [mailBox addAttachmentData:data mimeType:@"" fileName:@"随便填.jpg"];
    //设置代理人
    mailBox.mailComposeDelegate = self;
    [self presentViewController:mailBox animated:YES completion:^{
        
        
    }];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    NSString *message;
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"取消发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSaved:
            message = @"保存";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSent:
            message = @"已经发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultFailed:
            message = @"发送失败";
            NSLog(@"%@", message);
            
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}



//缓存
#if 1

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        NSLog(@"0");
    }else if (buttonIndex == 1) {
        // 创建一个文件管理者
//        NSFileManager *manager = [N SFileManager defaultManager];
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
       [self clearCache:cachePath];
        
    }
    
    
}
-(void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}

- (float )folderSizeAtPath:(NSString *)folderPath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath])
        return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}

- (long long)fileSizeAtPath:(NSString *)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
#endif

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
